package tw.zld369504.wol.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.lang.ref.WeakReference;

import tw.zld369504.wol.utils.Constant;
import tw.zld369504.wol.action.MainActivityController;
import tw.zld369504.wol.R;
import tw.zld369504.wol.base.BaseActivity;

public class MainActivity extends BaseActivity {

    private Button button;
    private MyHandler mHandler;
    private EditText editText_ip, editText_mac, editText_port;

    private MainActivityController mController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initViews();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mController.onDestroy();
    }

    private void initData() {
        mHandler = new MyHandler(this);
        mController = new MainActivityController(this);
        mController.onCreate(null);
    }

    private void initViews() {
        setContentView(R.layout.activity_main);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        button = (Button) findViewById(R.id.btn_wake);
        button.setOnClickListener(new ButtonImpl());
        editText_ip = (EditText) findViewById(R.id.editText1);
        editText_mac = (EditText) findViewById(R.id.editText2);
        editText_port = (EditText) findViewById(R.id.editText3);
    }

    private class ButtonImpl implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (v == button) {
                if (mController.validateInputData(editText_ip.getText().toString().trim(), editText_port.getText().toString().trim(), editText_mac.getText().toString().trim()))
                    mController.sendMagicPacket();
            }
        }
    }

    public MyHandler getHandler() {
        return this.mHandler;
    }

    public static class MyHandler extends Handler {

        WeakReference<MainActivity> mActivity;

        public MyHandler(MainActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity a = mActivity.get();
            if (a != null) {
                Constant state = Constant.values()[msg.what];
                switch (state) {
                    case STATE_SOCKET_OK:
                        a.dismissProgressDialog();
                        a.t(a.getString(R.string.operation_success));
                        break;
                    case STATE_IO_ERROR:
                    case STATE_SOCKET_FAILURE:
                    case STATE_OTHER_ERROR:
                        a.dismissProgressDialog();
                        a.t(msg.obj.toString());
                        break;
                    case STATE_IP_ERROR:
                        a.t(a.getString(R.string.err_input_ip));
                        break;
                    case STATE_MAC_ERROR:
                        a.t(a.getString(R.string.err_input_mac));
                        break;
                    case STATE_PORT_ERROR:
                        String errMessage=(String)msg.obj;
                        a.t(errMessage);
                        break;
                }
            }
        }
    }
}
